package com.classroompie.classroompieserver.service.impl;

import com.classroompie.classroompiecommon.constant.BackgroundConstant;
import com.classroompie.classroompiecommon.context.BaseContext;
import com.classroompie.classroompiecommon.utils.NumberGenerator;
import com.classroompie.classroompiepojo.entity.CourseModel;
import com.classroompie.classroompiepojo.entity.UserCourseModel;
import com.classroompie.classroompiepojo.entity.UserModel;
import com.classroompie.classroompiepojo.request.AddClassInput;
import com.classroompie.classroompiepojo.response.CourseDetailVO;
import com.classroompie.classroompieserver.core.UniqueStringGenerator;
import com.classroompie.classroompieserver.mapper.CourseMapper;
import com.classroompie.classroompieserver.mapper.UserCourseMapper;
import com.classroompie.classroompieserver.mapper.UserMapper;
import com.classroompie.classroompieserver.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.classroompie.classroompiepojo.entity.table.UserCourseModelTableDef.USER_COURSE_MODEL;


/**
 * @author jiahao
 * @since 2024/6/20 周四
 */
@Service
public class CourseServiceImpl implements CourseService {
    private final UniqueStringGenerator uniqueStringGenerator;

    private final UserCourseMapper userCourseMapper;

    private final CourseMapper courseMapper;

    private final UserMapper userMapper;

    public CourseServiceImpl(UniqueStringGenerator uniqueStringGenerator, UserCourseMapper userCourseMapper, CourseMapper courseMapper, UserMapper userMapper, UserMapper userMapper1) {
        this.uniqueStringGenerator = uniqueStringGenerator;
        this.userCourseMapper = userCourseMapper;
        this.courseMapper = courseMapper;
        this.userMapper = userMapper;
    }

    @Override
    @Transactional
    public void addCourse(AddClassInput addClassInput) {
        String code = UniqueStringGenerator.generateUniqueString();
        CourseModel courseModel = new CourseModel();
        courseModel.setBg(BackgroundConstant.BACKGROUND + NumberGenerator.generateCourseBGNumber() + ".png");
        courseModel.setCode(code);
        UserModel user = userMapper.selectOneById(BaseContext.getCurrentId());
        courseModel.setCreateUser(user.getName());
        BeanUtils.copyProperties(addClassInput, courseModel);
        courseModel.setAcademicYear(addClassInput.getAcademicYear());
        courseMapper.insert(courseModel);
        String id = courseModel.getId();
        UserCourseModel userCourseModel = new UserCourseModel();
        userCourseModel.setCourseId(id);
        userCourseModel.setUserId(BaseContext.getCurrentId());
        userCourseModel.setIdentity("老师");
        userCourseMapper.insertSelective(userCourseModel);
    }

    @Override
    public CourseDetailVO getCourse(String id) {
        CourseModel courseModel = courseMapper.selectOneById(id);
        CourseDetailVO courseDetailVO = new CourseDetailVO();
        BeanUtils.copyProperties(courseModel, courseDetailVO);
        int size = userCourseMapper.selectListByCondition(USER_COURSE_MODEL.COURSE_ID.eq(id)).size();
        courseDetailVO.setMemberCount(size);
        return courseDetailVO;
    }

    @Override
    public void topCourse(String courseId) {
        String currentId = BaseContext.getCurrentId();
        UserCourseModel userCourseModel = userCourseMapper.selectOneByCondition(USER_COURSE_MODEL.COURSE_ID.eq(courseId).and(USER_COURSE_MODEL.USER_ID.eq(currentId)));
        userCourseModel.setIsTop(!userCourseModel.getIsTop());
        userCourseMapper.update(userCourseModel);
    }

    @Override
    public void archiveCourse(String courseId) {
        String currentId = BaseContext.getCurrentId();
        UserCourseModel userCourseModel = userCourseMapper.selectOneByCondition(USER_COURSE_MODEL.COURSE_ID.eq(courseId).and(USER_COURSE_MODEL.USER_ID.eq(currentId)));
        userCourseModel.setIsArchived(!userCourseModel.getIsArchived());
        userCourseMapper.update(userCourseModel);
    }

}
